//
//  BoxControl.m
//  iCodeMac
//
//  Created by Andrew Barry on 5/04/09.
//  Copyright 2009 Barry Computer Services. All rights reserved.
//

#import "BoxControl.h"
#import "FlippedView.h"
#import "JavascriptForm.h"

@implementation BoxControl

-(NSArray *) getEvents {
	return [NSArray arrayWithObjects:@"Click", nil];
}

-(NSView *) load: (NSDictionary *) defn withFrame: (NSRect) frame {
	NSString *caption = [defn objectForKey:@"caption"];
	if (!caption)
		caption = @"box";
	dataSourceLoadName = [defn objectForKey:@"dataSource"];
	dataField = [defn objectForKey:@"dataField"];
	NSBox *vw = [[NSBox alloc] initWithFrame:frame];
	[vw setContentView:[FlippedView new]];
	[vw setTitle:caption];
	return vw;
}

-(id) valueForProperty: (NSString *) name {
	if ([name isEqualToString:@"caption"]) {
		return [(NSBox *) view title];
	} else if ([name isEqualToString:@"dataSource"]) {
		return dataSource;
	} else if ([name isEqualToString:@"dataField"]) {
		return dataField;
	}
	return [super valueForProperty: name];
}

-(void) setValue: (id) val forProperty:(NSString*) name {
	if ([name isEqualToString:@"caption"]) {
		[(NSBox *) view setTitle: (NSString *) val];
	} else if ([name isEqualToString:@"dataSource"]) {
		dataSource = val;
	} else if ([name isEqualToString:@"dataField"]) {
		[dataField autorelease];
		dataField = [val retain];
	} else {
		[super setValue: val forProperty: name];
	}
}

-(void) saveFields: (NSMutableDictionary *) content {
	[content setObject: [(NSButton *) view title] forKey: @"caption"];
	if (dataSource != nil && dataSource != [NSNull null]) {
		[content setObject: [dataSource controlName] forKey: @"dataSource"];
	}
	if (dataField)
		[self saveField: content forKey: @"dataField"];
	[super saveFields: content];
}

-(void) postLoad {
	FormEditor *editor = [formDocument editor];
	if ([dataSourceLoadName length] > 0) {
		dataSource = [editor controlByName:dataSourceLoadName];
	}
	
	[super postLoad];
}

-(NSString *) propsNibName {
	return @"BoxProps";
}

@end
